import axios from 'axios'
import React, { useRef } from 'react'
import { useNavigate } from 'react-router-dom'
export default function Home() {
    let navigate = useNavigate()
    let username = useRef()
    let password = useRef()
    let login = async () => {
        // 将用户名和密码返回
        let obj = {
            name: username.current.value,
            password: password.current.value
        }
        let { token, code } = (await axios.post("/login", obj)).data
        if (code === 403) {
            alert("不存在该用户")
            return
        } else {
            let { accessToken, refreshToken } = token
            // 将token存储到本地
            localStorage.setItem("AccessToken", accessToken)
            localStorage.setItem("RefreshToken", refreshToken)
            // 跳转到列表页面
            navigate("/list")
        }
        

    }
    return (
        <div>
            {/* 点击登录后端发送过来双token */}
            <h1>登录</h1>
            <p>
                <p><span>用户名字：<input ref={username} placeholder='请输入'></input></span></p>
                <p><span>用户密码：<input ref={password} type='password' placeholder='请输入'></input></span></p>
                <button onClick={login}>登录</button>
            </p>
        </div>
    )
}
